﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using System.IO;
using System.Windows.Forms;



namespace eRechnung_3_0
{
    public class ebInterfaceTableManager
    {
        private DataSet myDs;
        private DataSet myDs2;
        private DataTable ISO3166Table;
        private DataTable DocTypeTable;
        
        public ebInterfaceTableManager()
        {
            myDs = new DataSet();
            Assembly Asm = Assembly.GetExecutingAssembly();
            Stream St = Asm.GetManifestResourceStream("eRechnung_3_0.iso_3166-1_laender.xml");
            StreamReader ResRd = new StreamReader(St);
            myDs.ReadXml(ResRd);
            ISO3166Table = myDs.Tables["Row"];
            
            Stream St2 = Asm.GetManifestResourceStream("eRechnung_3_0.DocumentType.xml");
            StreamReader ResRd2 = new StreamReader(St2);
            myDs2 = new DataSet();
            myDs2.ReadXml(ResRd2);
            DocTypeTable = myDs2.Tables["Language"];
            String[] test = GetAllGerDocTypes();
            string eng = FindDocTypeEnglish(test[0]);
            return;
        }

        public String[] GetAllGerDocTypes()
        {
            String[] retval = new String[DocTypeTable.Rows.Count];
            int i = 0;
            foreach (DataRow item in DocTypeTable.Rows)
            {
                retval[i++] = item["german"].ToString();
            }
            return (retval);
        }

        public string FindDocTypeGerman(string doctype)
        {
            if (doctype == "")
            {
                return ("");
            }
            string selection = "english = '" + doctype + "'";
            DataRow[] rows = DocTypeTable.Select(selection);
            if (rows == null)
            {
                return (null);
            }
            return (rows[0]["german"].ToString());
        }

        public string FindDocTypeEnglish(string ger)
        {
            if (ger == "")
            {
                return ("");
            }
            string selection = "german = '" + ger + "'";
            DataRow[] rows = DocTypeTable.Select(selection);
            if (rows == null)
            {
                return (null);
            }
            return (rows[0]["english"].ToString());
        }

        public String[] GetAllCountries()
        {
            DataRow[] rows = ISO3166Table.Select();
            StringCollection tcoll = new StringCollection();
            string land = string.Empty;
            foreach (DataRow zeile in rows)
            {
                land = zeile["Land"].ToString();
                tcoll.Add(land);
            }
            String[] retval = new String[tcoll.Count];
            tcoll.CopyTo(retval, 0);
            return (retval);
        }

        public string FindLand(string shortcut)
        {
            if (shortcut=="")
            {
                return ("");
            }
            string selection = "Shortcut = '" + shortcut + "'";
            DataRow[] rows = ISO3166Table.Select(selection);
            if (rows == null)
            {
                return (null);
            }
            return(rows[0]["Land"].ToString());

        }

        public string FindShortcut(string land)
        {
            if (land=="")
            {
                return ("");
            }
            string selection = "Land = '" + land + "'";
            DataRow[] rows = ISO3166Table.Select(selection);
            if (rows == null)
            {
                return (null);
            }
            return(rows[0]["Shortcut"].ToString());

        }

        public void FillCountryCCbox(ComboBox lb)
        {
            String[] Countries = this.GetAllCountries();
            lb.Items.Clear();
            foreach (string land in Countries)
            {
                lb.Items.Add(land);
            }
            //            LiBxBankCountry.Items.AddRange(Countries);
            int i = Array.IndexOf(Countries, "Österreich");

            if (lb.Text.ToString() == "")
            {
                lb.Text = "Österreich";
            }
        }


    }
}
